package com.shanghaiyimu.yimiyangche.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.BaoyangShopselectAListViewdapter;
import com.shanghaiyimu.yimiyangche.bean.BaoYnagSelectShopBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class BaoYangShopSelectActivity extends AppCompatActivity implements View.OnClickListener {

//    private LinearLayout mLinearLayout;
    private ListView mListView,poplistviewdiqu;
    private BaoyangShopselectAListViewdapter aListViewdapter;
    private List<BaoYnagSelectShopBean> datas = new ArrayList<>();
    private List<BaoYnagSelectShopBean> datas2 = new ArrayList<>();
    private TextView txt1,txt2,txt3,txt4_moren,txt4_juli;
    private PopupWindow popupWindowDiqu,popupWindowXuanze;
    private View view1,view2;
    private ImageView to_map_activity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bao_yang_shop_select);
        initView();
        initData();
        initSetListener();
    }

    private void initSetListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(BaoYangShopSelectActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show();
                startActivity(new Intent(BaoYangShopSelectActivity.this,BaoYangActivity.class));
            }
        });
        popupWindowDiqu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                txt1.setTextColor(Color.RED);
            }
        });
    }

    private void initData() {
        BaoYnagSelectShopBean shopBean = new BaoYnagSelectShopBean();
        shopBean.setName("宜牧养车工厂店");
        shopBean.setAddress("兴庆公园北门");
        shopBean.setAssess(5);
        shopBean.setDistance(1.3);
        datas.add(shopBean);
        BaoYnagSelectShopBean shopBean1 = new BaoYnagSelectShopBean();
        shopBean1.setName("宜牧养车工厂店1");
        shopBean1.setAddress("兴庆公园北门1");
        shopBean1.setAssess(4);
        shopBean1.setDistance(1.7);
        datas.add(shopBean1);
        BaoYnagSelectShopBean shopBean2 = new BaoYnagSelectShopBean();
        shopBean2.setName("宜牧养车工厂店2");
        shopBean2.setAddress("兴庆公园北门2");
        shopBean2.setAssess(3);
        shopBean2.setDistance(6.3);
        datas.add(shopBean2);
        BaoYnagSelectShopBean shopBean3 = new BaoYnagSelectShopBean();
        shopBean3.setName("宜牧养车工厂店3");
        shopBean3.setAddress("兴庆公园北门3");
        shopBean3.setAssess(2);
        shopBean3.setDistance(2.3);
        datas.add(shopBean3);
        BaoYnagSelectShopBean shopBean4 = new BaoYnagSelectShopBean();
        shopBean4.setName("宜牧养车工厂店4");
        shopBean4.setAddress("兴庆公园北门4");
        shopBean4.setAssess(1);
        shopBean4.setDistance(1.1);
        datas.add(shopBean4);
        datas2.addAll(datas);
        aListViewdapter.notifyDataSetChanged();

    }

    private void initView() {
        to_map_activity = (ImageView) findViewById(R.id.map_img_activity);
        mListView = (ListView) findViewById(R.id.baoyang_select_shop_lv);
        aListViewdapter = new BaoyangShopselectAListViewdapter(this,datas);
        mListView.setAdapter(aListViewdapter);
        txt1 = (TextView) findViewById(R.id.diqu_select_baoyang_shop);
        txt2 = (TextView) findViewById(R.id.paxu_select_baoyang_shop);
        txt3 = (TextView) findViewById(R.id.pingjia_select_baoyang_shop);
        txt1.setOnClickListener(this);
        txt2.setOnClickListener(this);
        txt3.setOnClickListener(this);
        to_map_activity.setOnClickListener(this);

        view1 = LayoutInflater.from(this).inflate(R.layout.popwindowdiqu, null);
        popupWindowDiqu = new PopupWindow(view1, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        poplistviewdiqu = (ListView) view1.findViewById(R.id.popwindow_diqu_lv);

        poplistviewdiqu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        poplistviewdiqu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = ((TextView) view).getText().toString();
                txt1.setText(s);
                popupWindowDiqu.dismiss();
            }
        });


        view2 = LayoutInflater.from(this).inflate(R.layout.baoyangxuaze_item,null);
        view2.setOnClickListener(this);
        txt4_moren = (TextView) view2.findViewById(R.id.moren);
        txt4_juli = (TextView) view2.findViewById(R.id.juli);
        txt4_juli.setOnClickListener(this);
        txt4_moren.setOnClickListener(this);
        popupWindowXuanze = new PopupWindow(view2, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowXuanze.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.diqu_select_baoyang_shop:
                cleanTextColor();
                if (popupWindowDiqu == null||!popupWindowDiqu.isShowing()){
                    popupWindowDiqu.setTouchable(true);
                    popupWindowDiqu.setFocusable(true);
                    popupWindowDiqu.setOutsideTouchable(true);
                    popupWindowDiqu.setBackgroundDrawable(new ColorDrawable(0x99000000));
                    popupWindowDiqu.showAsDropDown(txt1);
                    txt1.setTextColor(Color.RED);
                }
                else
                {
                    popupWindowDiqu.dismiss();
                }
                break;
            case R.id.paxu_select_baoyang_shop:
                cleanTextColor();
                txt2.setTextColor(Color.RED);
                if (popupWindowXuanze == null||!popupWindowXuanze.isShowing()){
                    popupWindowXuanze.setTouchable(true);
                    popupWindowXuanze.setFocusable(true);
                    popupWindowXuanze.setOutsideTouchable(true);
                    popupWindowXuanze.setBackgroundDrawable(new ColorDrawable(0x99000000));
                    popupWindowXuanze.showAsDropDown(txt2);
                    txt2.setTextColor(Color.RED);
                }
                else
                {
                    popupWindowXuanze.dismiss();
                }
                break;
            case R.id.pingjia_select_baoyang_shop:
                cleanTextColor();
                txt3.setTextColor(Color.RED);
                Collections.sort(datas, new Comparator<BaoYnagSelectShopBean>() {
                    @Override
                    public int compare(BaoYnagSelectShopBean o1, BaoYnagSelectShopBean o2) {
                        if(o1.getAssess() > o2.getAssess()){
                            return -1;
                        }
                        if(o1.getAssess() == o2.getAssess()){
                            return 0;
                        }
                        return 1;
                    }
                });
                aListViewdapter.notifyDataSetChanged();


                break;
            case R.id.moren:
                txt4_moren.setTextColor(Color.RED);
                txt4_juli.setTextColor(Color.GRAY);
                txt2.setText(txt4_moren.getText());
                popupWindowXuanze.dismiss();
                datas.clear();
                datas.addAll(datas2);
                aListViewdapter.notifyDataSetChanged();
                break;
            case R.id.juli:
                txt4_juli.setTextColor(Color.RED);
                txt4_moren.setTextColor(Color.GRAY);
                txt2.setText( txt4_juli.getText());
                popupWindowXuanze.dismiss();
                Collections.sort(datas, new Comparator<BaoYnagSelectShopBean>() {
                    @Override
                    public int compare(BaoYnagSelectShopBean o1, BaoYnagSelectShopBean o2) {
                        if(o1.getDistance() > o2.getDistance()){
                            return 1;
                        }
                        if(o1.getDistance() == o2.getDistance()){
                            return 0;
                        }
                        return -1;
                    }
                });
                aListViewdapter.notifyDataSetChanged();
                break;
            case R.id.baoyangxuanze:
                popupWindowXuanze.dismiss();
                break;
            case R.id.map_img_activity:
                startActivity(new Intent(BaoYangShopSelectActivity.this,MapActivity.class));
                break;
        }
    }


    private List<String> getData(){

        List<String> data = new ArrayList<String>();
        data.add("未央区");
        data.add("雁塔区");
        data.add("碑林区");
        data.add("高新区");
        data.add("灞桥区");
        data.add("阎良区");
        data.add("临潼区");
        data.add("长安区");
        data.add("蓝田区");
        data.add("户县");
        data.add("高陵县");

        return data;
    }
    public void cleanTextColor(){
        txt1.setTextColor(Color.GRAY);
        txt2.setTextColor(Color.GRAY);
        txt3.setTextColor(Color.GRAY);
    }
}
